<template>
  <el-dialog
    v-dialogDrag
    @close="handleClose"
    :title="'导入'"
    :lock-scroll="false"
    :visible="showDialog"
    width="50%"
  >
    <el-form
      label-width="80px"
      :model="form"
      size="small"
      class="form"
      ref="formName"
    >
      <el-form-item
        label="模板："
        prop="templateId"
        :rules="[{ required: true, message: '请选择模板', trigger: 'change' }]"
      >
        <el-select
          class="w100"
          clearable
          v-model="form.templateId"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in templateListUniqBy"
            :key="item.token"
            :label="item.name + '-' + item.version"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="上传："
        prop="url"
        :rules="[{ required: true, message: '请选择文件', trigger: 'change' }]"
      >
        <app-upload
          tips=""
          customKey="xls"
          :is-resolution="true"
          @subUploadSucceed="handleUploadSucceed"
        >
          <el-button type="primary" size="small">点击上传</el-button>
        </app-upload>
        <span v-if="form.url">{{ form.url }}</span>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" size="mini" @click="handleClose"
        >取 消</el-button
      >
      <el-button
        :loading="btnLoading"
        size="mini"
        type="primary"
        @click="submitForm('formName')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { exportTechnology, submitImport } from "@/api/technology";
const baseURL = process.env.VUE_APP_BASE_URL;
import AppUpload from "@/views/device/devicegroups/UploadImage.vue";
export default {
  components: {
    AppUpload,
  },
  props: {
    showDialog: {
      type: Boolean,
      default: true,
    },
    templateList: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      fileList: [],
      btnLoading: false,
      form: {
        templateId: "",
        url: "",
      },
    };
  },
  computed: {
    action() {
      return (
        // /technology/formula/import/{id}
        baseURL +
        `/superlink/api/technology/formula/import/${this.form.templateId}`
      );
    },

    templateListUniqBy() {
      return _.uniqBy(this.templateList, "name");
    },
  },
  methods: {
    handleUploadSucceed(file) {
      this.form.url = file;
    },
    onSuccess() {
      this.$msg({
        type: "success",
        message: "导入成功!",
      });
      this.handleClose(true);
    },
    submitForm(formName) {
      if (!this.form.url) {
        this.$msg("请上传文件");
        return;
      }

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          submitImport(this.form)
            .then((res) => {
              this.onSuccess();
            })
            .finally((f) => {
              this.btnLoading = false;
            });
          // this.$refs.upload.submit();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(isRefresh) {
      this.cancelEmpty();
      this.$emit("update:showDialog", false);
      this.$emit("closeDialog", isRefresh);
    },
    cancelEmpty() {
      Object.assign(this.$data.form, this.$options.data().form);
      this.$refs.formName.resetFields();
    },
  },
};
</script>

<style>
</style>